﻿Imports System.Collections.Generic
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports TServerControl


Public Partial Class MyPager
	Inherits System.Web.UI.UserControl
	Protected Sub Page_Init(sender As Object, e As System.EventArgs)
		AddHandler Me.GV.DataBound, AddressOf Me.gv_DataBound
	End Sub

	Protected Sub Page_Load(sender As Object, e As System.EventArgs)
				'Me.BindddlCurrentPage()
				'Me.BindPageButton()
		If Not IsPostBack Then
		End If
	End Sub

	'protected void gv_PageIndexChanged(object sender, System.EventArgs e)
	'{
	'    GridView gv = (GridView)sender;

	'    this.BindddlCurrentPage(gv.PageIndex);
	'    this.BindPageButton();
	'}

	Protected Sub gv_DataBound(sender As Object, e As System.EventArgs)
		Dim gv As TGridView = DirectCast(sender, TGridView)
		If gv.PagerSettings.Position = PagerPosition.Bottom Then
			gv.BottomPagerRow.Visible = True
		End If
		If gv.PagerSettings.Position = PagerPosition.Top Then
			gv.TopPagerRow.Visible = True
		End If

		If gv.PagerSettings.Position = PagerPosition.TopAndBottom Then
			gv.BottomPagerRow.Visible = True
			gv.TopPagerRow.Visible = True
		End If



		Me.BindddlCurrentPage(gv.PageIndex)
		Me.BindPageButton()
	End Sub




	Protected Sub ddlCurrentPage_SelectedIndexChanged(sender As Object, e As System.EventArgs)
		Dim ddl As DropDownList = TryCast(sender, DropDownList)

		Dim args As New GridViewCommandEventArgs(ddl, New CommandEventArgs("Page", ddl.SelectedValue))
		Me.RaiseBubbleEvent(ddl, args)
	End Sub

	Protected Sub txtPagerSize_TextChanged(sender As Object, e As EventArgs)
		Try
			GV.PageSize = Int16.Parse(txtPagerSize.Text)
			GV.DataBind()
		Catch
		End Try

	End Sub
End Class

Partial Class MyPager
	Public Event PageButtonRefreshed As PageButtonRefreshedEventHandler
	Public Delegate Sub PageButtonRefreshedEventHandler(sender As Object, e As EventArgs)

	Private _GV As TGridView

	Private ReadOnly Property GV() As TGridView
		Get
			If _GV Is Nothing Then

				Dim parent As Control = Me.Parent

				While parent IsNot Nothing
					If TypeOf parent Is TGridView Then
						_GV = DirectCast(parent, TGridView)

						Return _GV
					End If

					parent = parent.Parent
				End While


				Return Nothing
			Else
				Return _GV
			End If
		End Get
	End Property

	Public ReadOnly Property TotalRowCount() As Integer
		Get
			Return GV.TotalRowCount
		End Get
	End Property



	Public Sub BindddlCurrentPage()
		BindddlCurrentPage(0)
	End Sub


	Public Sub BindddlCurrentPage(pageIndex As Integer)
		If GV Is Nothing Then
			Return
		End If

		If Me.ddlCurrentPage.Items.Count = 0 Then
			For i As Integer = 1 To Me.GV.PageCount
				Me.ddlCurrentPage.Items.Add(i.ToString())
			Next
		End If

		Me.ddlCurrentPage.Items(pageIndex).Selected = True
	End Sub

	Public Sub BindPageButton()
		If GV Is Nothing Then
			Return
		End If

		If Me.ddlCurrentPage.SelectedValue = "1" Then
			Me.btnFirst.Enabled = False
			Me.btnPrev.Enabled = False
		Else
			Me.btnFirst.Enabled = True
			Me.btnPrev.Enabled = True
		End If

		If Me.ddlCurrentPage.SelectedValue = Me.ddlCurrentPage.Items.Count.ToString() Then
			Me.btnLast.Enabled = False
			Me.btnNext.Enabled = False
		Else
			Me.btnLast.Enabled = True
			Me.btnNext.Enabled = True
		End If

		Me.lblRowCountInfo.Text = String.Format(GetLocalResourceObject("lblRowCountInfo").ToString(), Me.ddlCurrentPage.SelectedValue, Me.ddlCurrentPage.Items.Count, Me.TotalRowCount)
		Me.txtPagerSize.Text = GV.PageSize.ToString()
		RaiseEvent PageButtonRefreshed(Me, New EventArgs())
	End Sub

	Public Property RowCountInfo() As String
		Get
			Return Me.lblRowCountInfo.Text
		End Get
		Set
			Me.lblRowCountInfo.Text = value
		End Set
	End Property

	'public AjaxControlToolkit.ModalPopupExtender PopupExt
	'{
	'    get {return _PopupExt; }
	'    set { _PopupExt = value; }
	'}


End Class